import { RouteRecordRaw } from 'vue-router'
import { ROUTER_NAME } from '@/constant'
import microApp from '@micro-zoe/micro-app'

const modules = import.meta.globEager('./modules/**/*.ts')

const routeModuleList: RouteRecordRaw[] = []

// 判断是否是子应用
const microAppData = window.microApp?.getData() ?? window.eventCenterForViteApp?.getData()
Object.keys(modules).forEach(key => {
    const mod = modules[key].default || {}
    const modList = Array.isArray(mod) ? [...mod] : [mod]
    if (microAppData) {
        // 子应用不要外层结构
        const [children] = modList.map(item => item.children)
        // 兼容创建的应用中，路由没有children的情况
        if (children) {
            // 微应用前缀支持自定义传入
            const basePath = microAppData.baseroute || '/allin-app'

            const setBaseForPath = childrenMap => {
                childrenMap.forEach((item: any) => {
                    item.path = `${basePath}${item.path}`
                    if (item.children && item.children.length > 0) {
                        setBaseForPath(item.children)
                    }
                })
            }
            setBaseForPath(children)

            routeModuleList.push(...children)
        }
    } else {
        routeModuleList.push(...modList)
    }
})

microApp.start({
    "disable-memory-router": true, // 关闭虚拟路由系统
    "disable-patch-request": true, // 关闭对子应用请求的拦截
});

export const NOT_FOUND_PAGE: RouteRecordRaw = {
    name: ROUTER_NAME.PAGE_NOT_FOUND,
    // 匹配全部其他内容
    path: "/:productCode/:tenantCode" + import.meta.env.VITE_PUBLIC_PATH + '/:pathMatch(.*)*',
    component: () => import('@/views/system/PageNotFound.vue'),
    meta: {
        hideMenu: true,
        hideBreadcrumb: true,
        title: '404'
    }
}

export const NO_ACCESS_PAGE: RouteRecordRaw = {
    name: ROUTER_NAME.PAGE_NO_ACCESS,
    // 匹配全部其他内容
    path: "/:productCode/:tenantCode" + import.meta.env.VITE_PUBLIC_PATH + '/403',
    component: () => import('@/views/system/PageNoAccess.vue'),
    meta: {
        hideMenu: true,
        hideBreadcrumb: true,
        title: '403'
    }
}

export const LOGIN_PAGE: RouteRecordRaw = {
    name: ROUTER_NAME.PAGE_LOGIN,
    // 匹配全部其他内容
    path: "/:productCode/:tenantCode" + import.meta.env.VITE_PUBLIC_PATH + '/login',
    component: () => import('@/views/system/login/Login.vue'),
    meta: {
        hideMenu: true,
        hideBreadcrumb: true,
        title: 'Login'
    }
}

export const AUTH_LOGIN_PAGE: RouteRecordRaw = {
    path: "/:productCode/:tenantCode" + import.meta.env.VITE_PUBLIC_PATH + '/auth_login',
    name: ROUTER_NAME.AUTH_LOGIN,
    meta: {
        hideMenu: true,
        hideBreadcrumb: true,
        title: 'auth'
    },
    component: () => import('@/views/system/login/AuthorizeLogin.vue')
}


export const ROOT_PAGE: RouteRecordRaw = {
    path: "/:productCode/:tenantCode" + '/ROOT_PATH',
    name: ROUTER_NAME.PAGE_ROOT,
    redirect: "/:productCode/:tenantCode" + import.meta.env.VITE_PUBLIC_PATH + '/',
    meta: {
        title: 'Root',
        hideMenu: true,
        hideBreadcrumb: true
    }
}

export const asyncRoutes = [...routeModuleList, NOT_FOUND_PAGE, NO_ACCESS_PAGE]
